﻿using System;
using System.ComponentModel;
using System.Drawing;

namespace HML
{
    /// <summary>
    /// TabItem关闭按钮
    /// </summary>
    [TypeConverter(typeof(PropertyOrderConverter))]
    public class TabItemCloseButton
    {
        #region 属性

        private TabItemCloseButtonVisibles visible = TabItemCloseButtonVisibles.Auto;
        /// <summary>
        /// TabItem关闭按钮是否显示(Auto表示采用控件通用设置)
        /// </summary>
        [Description("TabItem关闭按钮是否显示(Auto表示采用控件通用设置)")]
        [PropertyOrder(-118)]
        [DefaultValue(TabItemCloseButtonVisibles.Auto)]
        public TabItemCloseButtonVisibles Visible
        {
            get { return this.visible; }
            set
            {
                if (this.visible == value)
                    return;

                this.visible = value;
                this.InitializeInvalidate();
            }
        }

        private bool enabled = true;
        /// <summary>
        /// TabItem使用状态是否已启动
        /// </summary>
        [Description("TabItem使用状态是否已启动")]
        [PropertyOrder(-116)]
        [DefaultValue(true)]
        public bool Enabled
        {
            get { return this.enabled; }
            set
            {
                if (this.enabled == value)
                    return;

                this.enabled = value;
                this.Invalidate();
            }
        }

        private Color backNormalColor = Color.Empty;
        /// <summary>
        /// TabItem按钮背景颜色(正常)
        /// </summary>
        [Description("TabItem按钮背景颜色(正常)")]
        [PropertyOrder(-114)]
        [DefaultValue(typeof(Color), "")]
        public Color BackNormalColor
        {
            get { return this.backNormalColor; }
            set
            {
                if (this.backNormalColor == value)
                    return;

                this.backNormalColor = value;
                this.Invalidate();
            }
        }

        private Color backEnterColor = Color.Empty;
        /// <summary>
        /// TabItem按钮背景颜色(鼠标进入)
        /// </summary>
        [Description("TabItem按钮背景颜色(鼠标进入)")]
        [PropertyOrder(-112)]
        [DefaultValue(typeof(Color), "")]
        public Color BackEnterColor
        {
            get { return this.backEnterColor; }
            set
            {
                if (this.backEnterColor == value)
                    return;

                this.backEnterColor = value;
                this.Invalidate();
            }
        }

        private Color foreNormalColor = Color.Empty;
        /// <summary>
        /// TabItem按钮前景颜色(正常)
        /// </summary>
        [Description("TabItem按钮前景颜色(正常)")]
        [PropertyOrder(-110)]
        [DefaultValue(typeof(Color), "")]
        public Color ForeNormalColor
        {
            get { return this.foreNormalColor; }
            set
            {
                if (this.foreNormalColor == value)
                    return;

                this.foreNormalColor = value;
                this.Invalidate();
            }
        }

        private Color foreEnterColor = Color.Empty;
        /// <summary>
        /// TabItem按钮前景颜色(鼠标进入)
        /// </summary>
        [Description("TabItem按钮前景颜色(鼠标进入)")]
        [PropertyOrder(-108)]
        [DefaultValue(typeof(Color), "")]
        public Color ForeEnterColor
        {
            get { return this.foreEnterColor; }
            set
            {
                if (this.foreEnterColor == value)
                    return;

                this.foreEnterColor = value;
                this.Invalidate();
            }
        }

        #region 用于临时存储运行信息

        /// <summary>
        /// 选项关闭按钮rect(包括外边距)
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal RectangleF M_Rect { get; set; }

        /// <summary>
        /// 选项关闭按钮rect(不包括内外边距)
        /// </summary>
        [Browsable(false)]
        [EditorBrowsable(EditorBrowsableState.Never)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        internal RectangleF C_Rect { get; set; }

        /// <summary>
        /// 自定义保存数据
        /// </summary>
        [DefaultValue(null)]
        [Browsable(false)]
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public object Data { get; set; }

        #endregion

        #endregion

        #region 字段

        /// <summary>
        /// TabItem关闭按钮所属TabPage
        /// </summary>
        private TabPagePlus owner;

        #endregion

        /// <summary>
        /// TabItem关闭按钮所属TabPage
        /// </summary>
        /// <param name="_owner"></param>
        public TabItemCloseButton(TabPagePlus _owner)
        {
            this.owner = _owner;
        }

        #region 私有方法

        /// <summary>
        /// 控件重绘
        /// </summary>
        private void Invalidate()
        {
            if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                tabControl.Invalidate();
            }
        }

        /// <summary>
        /// 控件重新初始化和重绘
        /// </summary>
        private void InitializeInvalidate()
        {
            if (this.owner != null && this.owner.Parent != null && this.owner.Parent is TabControlPlus)
            {
                TabControlPlus tabControl = (TabControlPlus)this.owner.Parent;
                tabControl.InitializeRectangle();
                tabControl.Invalidate();
            }
        }

        #endregion

    }

}
